/*
 *  Copyright (c) M2TK Project. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package m2tk.assistant.app.ui.component;

import com.google.common.eventbus.EventBus;
import m2tk.assistant.app.ui.event.DrawNetworkDiagramRequest;
import m2tk.assistant.app.ui.model.MultiplexInfoTableModel;
import m2tk.assistant.app.ui.util.ComponentUtil;
import m2tk.assistant.app.ui.util.ThreeStateRowSorterListener;
import m2tk.assistant.common.domain.SIMultiplex;
import m2tk.assistant.common.ui.RoundPanel;
import net.miginfocom.swing.MigLayout;
import org.kordamp.ikonli.fluentui.FluentUiRegularAL;
import org.kordamp.ikonli.swing.FontIcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableRowSorter;
import java.util.List;

import static m2tk.assistant.common.ui.UIConstants.ICON_SIZE_S;
import static m2tk.assistant.common.ui.UIConstants.LIGHT_BLUE;

public class NetworkInfoPane extends RoundPanel
{
    private static final Logger log = LoggerFactory.getLogger(NetworkInfoPane.class);

    private MultiplexInfoTableModel modelActualNW;
    private MultiplexInfoTableModel modelOtherNW;

    private EventBus eventBus;

    public NetworkInfoPane()
    {
        super();
        initUI();
    }

    private void initUI()
    {
        modelActualNW = new MultiplexInfoTableModel();
        modelOtherNW = new MultiplexInfoTableModel();

        TableRowSorter<MultiplexInfoTableModel> rowSorterActualNW = new TableRowSorter<>(modelActualNW);
        rowSorterActualNW.addRowSorterListener(new ThreeStateRowSorterListener(rowSorterActualNW));
        TableRowSorter<MultiplexInfoTableModel> rowSorterOtherNW = new TableRowSorter<>(modelOtherNW);
        rowSorterOtherNW.addRowSorterListener(new ThreeStateRowSorterListener(rowSorterOtherNW));

        JTable table1 = createTable();
        table1.setModel(modelActualNW);
        table1.setRowSorter(rowSorterActualNW);
        table1.getTableHeader().setReorderingAllowed(false);
        table1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JTable table2 = createTable();
        table2.setModel(modelOtherNW);
        table2.setRowSorter(rowSorterOtherNW);
        table2.getTableHeader().setReorderingAllowed(false);
        table2.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        DefaultTableCellRenderer leadingRenderer = new DefaultTableCellRenderer();
        leadingRenderer.setHorizontalAlignment(SwingConstants.LEADING);

        TableColumnModel columnModel = table1.getColumnModel();
        ComponentUtil.configTableColumn(columnModel, 0, leadingRenderer, 80, false);  // 序号
        ComponentUtil.configTableColumn(columnModel, 1, leadingRenderer, 360, true);  // 网络名称
        ComponentUtil.configTableColumn(columnModel, 2, leadingRenderer, 160, true);  // 传输系统
        ComponentUtil.configTableColumn(columnModel, 3, leadingRenderer, 160, true);  // 传输频点
        ComponentUtil.configTableColumn(columnModel, 4, leadingRenderer, 100, false); // 传输流号
        ComponentUtil.configTableColumn(columnModel, 5, leadingRenderer, 100, false); // 原始网络号
        ComponentUtil.configTableColumn(columnModel, 6, leadingRenderer, 100, false); // 业务数量

        columnModel = table2.getColumnModel();
        ComponentUtil.configTableColumn(columnModel, 0, leadingRenderer, 80, false);  // 序号
        ComponentUtil.configTableColumn(columnModel, 1, leadingRenderer, 360, true);  // 网络名称
        ComponentUtil.configTableColumn(columnModel, 2, leadingRenderer, 160, true);  // 传输系统
        ComponentUtil.configTableColumn(columnModel, 3, leadingRenderer, 160, true);  // 传输频点
        ComponentUtil.configTableColumn(columnModel, 4, leadingRenderer, 100, false); // 传输流号
        ComponentUtil.configTableColumn(columnModel, 5, leadingRenderer, 100, false); // 原始网络号
        ComponentUtil.configTableColumn(columnModel, 6, leadingRenderer, 100, false); // 业务数量

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("当前网络", createScrollPane(table1));
        tabbedPane.add("其他网络", createScrollPane(table2));

        setLayout(new MigLayout("insets 10", "[][grow][]", "[25!][pref!][][grow]"));
        add(new JLabel("网络结构",
                       StreamInfoNaviPane.BOARD_ICON,
                       SwingConstants.TRAILING), "left");
        add(createControlButton(FontIcon.of(FluentUiRegularAL.IMAGE_EDIT_20, ICON_SIZE_S, LIGHT_BLUE),
                                e ->
                                {
                                    if (eventBus != null)
                                        eventBus.post(new DrawNetworkDiagramRequest());
                                }),
            "skip, wrap");
        add(separator(), "span 3, grow, wrap");
        add(tabbedPane, "span 3 2, grow");
    }

    public void setEventBus(EventBus bus)
    {
        eventBus = bus;
    }

    public void updateMultiplexes(List<SIMultiplex> tsActualNW, List<SIMultiplex> tsOtherNW)
    {
        modelActualNW.update(tsActualNW);
        modelOtherNW.update(tsOtherNW);
    }
}
